<template>
	<view class="content">
		<xy-tabs :list="navList" v-model="tabCurrentIndex" @tab-click="tabClick"></xy-tabs>
		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view
					class="list-scroll-content"
					scroll-y
					@scrolltolower="loadData">
					<!-- 空白页 -->
					<xy-empty v-if="tabItem.orderList.length == 0"></xy-empty>
					<!-- 订单列表 -->
					<view
						:key="index"
						v-for="(item,index) in tabItem.orderList"
						@click="navTo('/pages/ishop/order/info?orderNos=' + item.orderNo)"
						class="order-item">
						<view class="i-top b-b">
							<text class="title">{{item.storeInfo.title}}</text>
							<text class="state">{{item.stateFormat.title}}</text>
						</view>
						<!-- O2O订单一般适合横向排列 -->
						<scroll-view v-if="item.listType && item.listType == 'o2o'" class="goods-box" scroll-x>
							<view
								v-for="(goodsItem, goodsIndex) in item.skuList" :key="goodsIndex"
								class="goods-item">
								<xy-image class="goods-img" v-show="goodsItem.cover" :src="goodsItem.cover" mode="aspectFill"></xy-image>
							</view>
						</scroll-view>
						<!-- 普通商城订单适合竖向排列 -->
						<view
							class="goods-box-single"
							v-for="(goodsItem, goodsIndex) in item.skuList" :key="goodsIndex">
							<xy-image class="goods-img" :src="goodsItem.cover" mode="aspectFill"></xy-image>
							<view class="right">
								<text class="title clamp">{{goodsItem.title}}</text>
								<view class="attr-box">
									<view class="spec">
										{{goodsItem.spec}}
									</view>
									<view class="amount">
										x {{goodsItem.amount}}
									</view>
								</view>
								<view class="price-box">
									<view class="price">{{goodsItem.price}}</view>
									<view class="state">
										<text class="text-error font-sm" v-if="goodsItem.state == 1" type="default" size="small">退货中</text>
										<text class="text-error font-sm" v-if="goodsItem.state == 2" type="default" size="small">已退货</text>
									</view>
								</view>
							</view>
						</view>
						<view class="deliver-info flex" style="color: #909399;font-size: 12px;">
							<xy-tag class="m-r-xs" :plain="true" shape="circle" size="small">{{item.deliverTypeFormat.title}}</xy-tag>
							<xy-tag class="m-r-xs" :plain="true" shape="circle" size="small" v-if="item.deliverPay">货到付款</xy-tag>
							<xy-tag class="m-r-xs" :plain="true" shape="circle" size="small" v-if="item.promoteType">{{item.promoteTypeFormat.title}}</xy-tag>
						</view>
						<view class="price-total">
							<view class="time">
								{{item.createTime|formatTime}}
							</view>
							<view class="summary">
								共
								<text class="num">{{item.skuList.length}}</text>
								件商品 实付款
								<text class="price">{{item.realMoney}}</text>
							</view>
						</view>
						<view class="action-box b-t" v-if="item.stateExtend.actionTitle && item.stateExtend.actionTitle.length > 0">
							<button
								:key="key"
								v-for="(action, key) in item.stateExtend.actionTitle"
								class="action-btn"
								:class="(action == '立即付款'
									|| action == '确认收货'
									|| action == '评价'
									? 'active' : '')"
								@click.stop="doAction(action, item.orderNo, item)">
								{{action}}
							</button>
						</view>
					</view>
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>

<script>
	export default {
		components: {
		},
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
						state: 'all',
						title: '全部',
						loadingType: 'more',
						orderList: [],
						page: 1
					},
					{
						state: 'waitpay',
						title: '待付款',
						loadingType: 'more',
						orderList: [],
						page: 1
					},
					{
						state: 'waitdeliver,waitpick,waitconfirm',
						title: '待收货',
						loadingType: 'more',
						orderList: [],
						page: 1
					},
					{
						state: 'waitcomment,done',
						title: '已完成',
						loadingType: 'more',
						orderList: [],
						page: 1
					},
					{
						state: 'return,service',
						title: '退货',
						loadingType: 'more',
						orderList: [],
						page: 1
					}
				]
			};
		},
		onLoad(options){
			if (options.tab) {
				this.tabCurrentIndex = parseInt(options.tab);
			}
			this.loadData();
		},
		async onPullDownRefresh() {
			await this.loadData();
			uni.stopPullDownRefresh();
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 5000);
		},
		methods: {
			//获取订单列表
			async loadData(source){
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				let state = navItem.state;

				if(source === 'tabChange' && navItem.loaded === true){
					//tab切换只有第一次需要加载数据
					return;
				}
				if(navItem.loadingType === 'loading'){
					//防止重复加载
					return;
				}
				navItem.loadingType = 'loading';

				// 获取数据
				let res = await this.util.request({
					url: '/v1/ishop/order/my',
					method: 'get',
					data: {
						state: navItem.state,
						page: navItem.page
					}
				});
				if (res.code == 200) {
					navItem.orderList = res.data.dataList;

					// loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true);

					// 判断是否还有数据， 有改为 more， 没有改为noMore
					navItem.loadingType = 'more';
				}
			},
			// swiper切换
			changeTab(e){
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			// 顶部tab点击
			tabClick(index){
				this.tabCurrentIndex = index;
			},
			// 恢复loaded
			resetLoaded(){
				let list = this.navList;
				for (var i = 0; i < list.length; i++) {
					list[i].loaded = false;
				}
			},
			navTo(url){
				uni.navigateTo({
					url: url
				})
			},
			async doAction(stateTitle, orderNo, item) {
				uni.showLoading({
					title: '请稍后'
				});
				var res = {};
				switch (stateTitle){
					case '删除订单':
						uni.showModal({
							content: '确定要删除订单？',
							success: async (e)=>{
								if (e.confirm) {
									res = await this.util.request({
										url: '/v1/ishop/order/delete/' + orderNo,
										method: 'DELETE'
									});
									if (res.code == 200) {
										this.util.showToast({
											title: res.msg,
											icon: 'success',
											duration: 1000
										});
										this.loadData();
										this.resetLoaded();
									} else {
										this.util.showToast({
											title: res.msg,
											icon: 'loading',
											duration: 1500
										});
									}
								}
							}
						});
						break;
					case '取消订单':
						uni.showModal({
							content: '确定要取消订单？',
							success: async (e)=>{
								if (e.confirm) {
									res = await this.util.request({
										url: '/v1/ishop/order/cancel',
										method: 'PUT',
										data: {
											orderNo: orderNo
										}
									});
									if (res.code == 200) {
										this.util.showToast({
											title: res.msg,
											icon: 'success',
											duration: 1000
										});
										this.loadData();
										this.resetLoaded();
									} else {
										this.util.showToast({
											title: res.msg,
											icon: 'loading',
											duration: 1500
										});
									}
								}
							}
						});
						break;
					case '立即付款':
						// 跳转付款页面
						this.navTo('/pages/ishop/order/info?orderNos=' + orderNo);
						break;
						return;
					case '确认收货':
						uni.showModal({
							content: '确定要确认收货？',
							success: async (e)=>{
								if (e.confirm) {
									res = await this.util.request({
										url: '/v1/ishop/order/confirm',
										method: 'PUT',
										data: {
											orderNo: orderNo
										}
									});
									if (res.code == 200) {
										this.util.showToast({
											title: res.msg,
											icon: 'success',
											duration: 1000
										});
										this.loadData();
										this.resetLoaded();
									} else {
										this.util.showToast({
											title: res.msg,
											icon: 'loading',
											duration: 1500
										});
									}
								}
							}
						});
						break;
					case '评价':
					    // 跳转评价页面
						uni.setStorageSync('ishopCommmentCurrent', item);
						this.navTo('/pages/ishop/comment/add?orderNo=' + orderNo);
					    break;
					    return;
					case '退货':
					    // 跳转退货页面
						this.navTo('/pages/ishop/order_return/apply?orderNo=' + orderNo);
						break;
					    return;
					case '售后':
					    // 跳转售后页面
					    break;
					    return;
				}
				uni.hideLoading();
			}
		}
	}
</script>

<style lang="scss">
	page, .content {
		background: $page-color-base;
		height: 100%;
	}
	.swiper-box {
		height: calc(100% - 40px);
	}
	.list-scroll-content {
		height: 100%;
	}
	.uni-swiper-item {
		height: auto;
	}
	.order-item {
		display: flex;
		flex-direction: column;
		padding-left: 30upx;
		background: #fff;
		margin-top: 16upx;
		.i-top {
			display: flex;
			align-items: center;
			height: 80upx;
			padding-right:30upx;
			font-size: $font-base;
			color: $font-color-dark;
			position: relative;
			.title {
				flex: 1;
			}
			.state {
				color: $uni-color-primary;
			}
			.del-btn {
				padding: 10upx 0 10upx 36upx;
				font-size: $font-lg;
				color: $font-color-light;
				position: relative;
				&:after {
					content: '';
					width: 0;
					height: 30upx;
					border-left: 1px solid $border-color-dark;
					position: absolute;
					left: 20upx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
		.action-box{
			display: flex;
			justify-content: flex-end;
			align-items: center;
			height: 100upx;
			position: relative;
			padding-right: 30upx;
			.action-btn {
				width: 160upx;
				height: 56upx;
				margin: 0;
				margin-left: 24upx;
				padding: 0;
				text-align: center;
				line-height: 56upx;
				font-size: $font-sm + 2upx;
				color: $font-color-dark;
				background: #fff;
				border-radius: 100px;
				&:after{
					border-radius: 100px;
				}
				&.active {
					background: #fff9f9;
					color: $uni-color-primary;
					&:after{
						border-color: #f7bcc8;
					}
				}
			}
		}
	}


	/* load-more */
	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80upx;
		align-items: center;
		justify-content: center
	}

	.uni-load-more__text {
		font-size: 28upx;
		color: #999
	}

	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}

	.uni-load-more__img>view {
		position: absolute
	}

	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}

	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}

	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}

	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}

	.load2 {
		transform: rotate(30deg)
	}

	.load3 {
		transform: rotate(60deg)
	}

	.load1 view:nth-child(1) {
		animation-delay: 0s
	}

	.load2 view:nth-child(1) {
		animation-delay: .13s
	}

	.load3 view:nth-child(1) {
		animation-delay: .26s
	}

	.load1 view:nth-child(2) {
		animation-delay: .39s
	}

	.load2 view:nth-child(2) {
		animation-delay: .52s
	}

	.load3 view:nth-child(2) {
		animation-delay: .65s
	}

	.load1 view:nth-child(3) {
		animation-delay: .78s
	}

	.load2 view:nth-child(3) {
		animation-delay: .91s
	}

	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}

	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}

	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}

	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}

	@-webkit-keyframes load {
		0% {
			opacity: 1
		}

		100% {
			opacity: .2
		}
	}
</style>
